﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using yq.Dictionary;
using Db;

namespace yq.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Sys : yq.IService.ISys
    {
        /// <summary>
        /// 系统是否需呀初始化
        /// </summary>
        /// <returns></returns>
        public yq.Dictionary.ReturnValue<string> Initialize()
        {
            yq.Dictionary.ReturnValue<string> rv = new ReturnValue<string>();
            string id = "";

            Func<DbHelper, yq.CaCheRefresh, bool> dbfun = (cn, ca) =>
            {
                //初始化系统
                yq.Business.SysInfoDal dal = new Business.SysInfoDal();
                var list = dal.GetAllList(cn);
                if (list.Count == 0)
                {
                    yq.Model.Logic.SysInfo info = new Model.Logic.SysInfo()
                    {
                        FileUp = 20000000,
                        Logo = "",
                        Title = "我们的社区",
                        WelCome = ""
                    };
                    dal.Add(info, cn);
                    id = info.WebId;
                }
                else
                {
                    id = list[0].WebId;
                }

                //是否注册了系统管理员
                var model = yq.Model.Logic.UserAccount.Load(yq.Dictionary.SysPromise.AdminId, Basis.CaCheLocdType.Db);

                cn.Commit();

                return model.IsLoad;
            };

            if (Db.DBTrans.DoTrans(dbfun))
            {
                rv.True(id);
            }
            else
            {
                rv.False();
            }

            return rv;
        }

        /// <summary>
        /// 修改配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditInfo(Model.Logic.SysInfo model)
        {
            Func<DbHelper, yq.CaCheRefresh, bool> dbfun = (cn, ca) =>
            {
                new yq.Business.SysInfoDal().Update(model, cn);

                cn.Commit();

                return true;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }



        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public ReturnValue<string> AddTask(Model.Logic.SysTask task)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                task.CreateTtime = DateTime.Now;

                new yq.Business.SysTaskDal().Add(task, cn);
                cn.Commit();
                rv.True(task.Rowid);

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 编辑任务
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public ReturnValue<string> EditTask(Model.Logic.SysTask task)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                var model=new yq.Business.SysTaskDal();

                if (model.GetCount(obj => obj.State == Enums.SysTask.State.Run && obj.Rowid == task.Rowid, cn) != 0)
                {
                    rv.False("任务运行中,请稍后更新");
                }else if (new yq.Business.SysTaskDal().Update(task, cn) == 0)
                {
                    rv.False("更新错误");
                }
                else
                {
                    cn.Commit();
                    rv.True(task.Rowid);
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public ReturnValue<string> DelTask(string  taskId)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                var dal = new yq.Business.SysTaskDal();
                var model = dal.GetModel(obj => obj.Rowid == taskId,cn);

                if (model.State == Enums.SysTask.State.Run)
                {
                    rv.False("任务运行中，不能删除");
                    cn.Rollback();
                }
                else
                {
                    dal.Delete(taskId,cn);
                    cn.Commit();
                    rv.True(taskId);
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 查询任务列表
        /// </summary>
        /// <returns></returns>
        public IList<Model.Logic.SysTask> SelectTaskAllList()
        {
            return new yq.Business.SysTaskDal().GetAllList(new Db.DbHelper());
        }
    }
}
